import os
import shutil

# 遍历文件夹，删除重复文件
def dedupe_folder(folder_path):

    """
    删除文件夹下重复文件
    :param folder_path:
    :return:
    """

    file_list = os.listdir(folder_path)

    allFolder = {}

    for item in file_list:
        sonPath = os.path.join(folder_path, item)

        for sonItem in os.listdir(sonPath):

            if sonItem in allFolder:
                sonSonPath = os.path.join(sonPath, sonItem)
                print(f'{sonSonPath} has been removed')
                shutil.rmtree(sonSonPath)  # 删除非空的文件夹
            else:
                allFolder[sonItem] = True
                # print(f'{sonItem} has been added to the list')

# 遍历文件夹,查找重复文件夹名称
def dedupe_folder_name(folder_path,folder_name):
    """
    删除文件夹下重复文件夹名称
    :param folder_path:
    :return:
    """
    file_list = os.listdir(folder_path)

    allFolder = {}

    for item in file_list:
        sonPath = os.path.join(folder_path, item)
        for sonItem in os.listdir(sonPath):
            # print(sonItem,folder_name)
            if sonItem == folder_name:
                return {
                    "status": True,
                    "path": os.path.join(sonPath, sonItem)
                }

    return {
        "status": False
    }


if __name__ == '__main__':
    folder_path = f'C:\\Users\\liuxi\\Desktop\\摄影学习\\02 样片'
    dedupe_folder(folder_path)